package ece496.sensorService;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIOFactory;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.api.exception.IncompatibilityException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class SensorService extends Service {

    private float sensorValue = 0;
    boolean connected = false;
    IOIO ioio;
    IOIOThread t = new IOIOThread();
    AnalogInput sensorIn;
    int obstacleDetected = 0;
    //0 = no obstacle
    //1 = obstacle on the left
    //2 = obstacle on the right
    //3 = obstacle on the centre
    //1.5 = close
    //1.7 = further
    //1.9 = very close
    //2.5 = close, 2.7 = further 2.9 = very close

    @Override
    public IBinder onBind(Intent arg0) {
        Log.d(getClass().getSimpleName(), "onBind()");
        return mySensorServiceStub;
    }
    private ISensorRemoteService.Stub mySensorServiceStub = new ISensorRemoteService.Stub() {

        public float getValue() throws RemoteException {
            try {
                sensorValue = sensorIn.read();
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Unexpected exception caught", e);
            }
            return sensorValue;
        }

        ;
		public String returnSt() throws RemoteException {
			String out =  Double.toString(t.getlastreading(0)) + " " +
						  Double.toString(t.getlastreading(1)) + " " +
						  Double.toString(t.getlastreading(2));
			return out;
        }

        ;
        
        public double return_mon() throws RemoteException {
        	return t.battery();
        }
        
        ;
        
	public void stopThread() throws RemoteException {
            t.finish();
        }

        ;


		public void sensorControl(int flag) {
            //if (flag & 0x1) ;	//only do middle one
            //if (flag & 0x11); //take both values
            //else if (flag & 0x10); //take only values from the side
        }
    ;

    };

	@Override
    public void onCreate() {
        super.onCreate();
        Log.d(getClass().getSimpleName(), "creating IOIO");
        ioio = IOIOFactory.create();
        Log.d(getClass().getSimpleName(), "created IOIO");
        try {
            ioio.waitForConnect();
            connected = true;
        } catch (ConnectionLostException ce) {
            Log.e(getClass().getSimpleName(), "Connection Lost exception caught", ce);
        } catch (IncompatibilityException ie) {
            Log.e(getClass().getSimpleName(), "Interrupted exception caught", ie);
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Unexpected exception caught", e);
            ioio.disconnect();
        }
        Log.d(getClass().getSimpleName(), "onCreate()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        sensorIn.close();
        ioio.disconnect();
        Log.d(getClass().getSimpleName(), "onDestroy()");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        try {
            sensorIn = ioio.openAnalogInput(35);
        } catch (ConnectionLostException ce) {
            Log.e(getClass().getSimpleName(), "Connection Lost exception caught", ce);
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Unexpected exception caught", e);
        }
        startThread();
        Log.d(getClass().getSimpleName(), "onStart()");

    }

    private void startThread() {
        if (!t.done()) {
            t.init(ioio);
            Log.d("IOIO", "Starting thread");
            t.start();
            Log.d("IOIO", "Started thread");
        }
    }
}